package com.youmu.sichuansfc.dao.impl;

import com.youmu.sichuansfc.dao.FundManagerFundDao;
import com.youmu.sichuansfc.domain.YoumuFundManagerFund;
import com.youmu.sichuansfc.domain.pojo.SearchYoumuFundManagerFund;
import com.youmu.sichuansfc.dto.PageT;
import com.youmu.sichuansfc.dto.fund.FundManagerFundInvestDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基金管理人 下的基金
 *
 * @author WYZ
 * @create 2018-01-30 20:11
 * @desc
 **/
@Repository
public class FundManagerFundDaoImpl implements FundManagerFundDao {

    private EntityManager em;

    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public YoumuFundManagerFund upload(YoumuFundManagerFund YoumuFundManagerFund) {
        if (YoumuFundManagerFund != null && YoumuFundManagerFund.getId() != null) {
            em.merge(YoumuFundManagerFund);
        } else {
            em.persist(YoumuFundManagerFund);
        }
        return YoumuFundManagerFund;
    }

    @Override
    public YoumuFundManagerFund findById(long id) {
        String sql = "from YoumuFundManagerFund where is_delete = 0 and id =:id";

        List<YoumuFundManagerFund> YoumuFundManagerFundList = em.createQuery(sql, YoumuFundManagerFund.class)
                .setParameter("id", id)
                .getResultList();
        if (YoumuFundManagerFundList != null && YoumuFundManagerFundList.size() > 0) {
            return YoumuFundManagerFundList.get(0);
        }
        return null;
    }

    @Override
    public YoumuFundManagerFund fundUserfindfund(long fundUserId, long id) {

        StringBuffer sql = new StringBuffer();

        sql.append("SELECT * FROM \n");
        sql.append("youmu_fund_manager_fund WHERE \n");
        sql.append("AND parent_id IS NULL \n");
        sql.append("AND is_delete = 0 \n");
        sql.append("and fund_user_id =:fund_user_id \n");

        List<YoumuFundManagerFund> youmuFundManagerFundList = em.createNativeQuery(sql.toString(), YoumuFundManagerFund.class)
                .setParameter("fundUserId", fundUserId)
                .getResultList();
        if (youmuFundManagerFundList != null && youmuFundManagerFundList.size() > 0) {
            return youmuFundManagerFundList.get(0);
        }
        return null;
    }

    @Override
    public PageT findBy(Integer fundType, String searchName, int num, int size) {
        Map<String, Object> map = new HashMap<>();

        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT yfmf.*, yfm.manager_name from \n");
        sql.append(" youmu_fund_manager_fund yfmf left join youmu_fund_manager yfm \n");
        sql.append(" on yfm.id = yfmf.fund_manager_id \n");
        sql.append(" where yfmf.is_delete = 0 and yfmf.is_delete = 0 \n");

        if (fundType != null) {
            sql.append(" and fund_type =:fundType \n");
            map.put("fundType", fundType);
        }
        if (StringUtils.isNotEmpty(searchName)) {
            sql.append(" and ( yfmf.fund_name like :searchFundName or yfm.manager_name like :searchManagerName ) \n");
            map.put("searchFundName", "%" + searchName + "%");
            map.put("searchManagerName", "%" + searchName + "%");
        }
        sql.append(" order by yfmf.add_time desc \n");

        javax.persistence.Query query = em.createNativeQuery(sql.toString(), SearchYoumuFundManagerFund.class);

        if (map != null) {
            for (String key : map.keySet()) {
                query.setParameter(key, map.get(key));
            }
        }

        PageT pageT = new PageT();
        pageT.setCount(query.getResultList().size());
        pageT.setPageList(query.setFirstResult(num * size)
                .setMaxResults(size)
                .getResultList());
        return pageT;
    }

    @Override
    public SearchYoumuFundManagerFund findById2(Long id) {

        StringBuffer sql = new StringBuffer();
        sql.append("SELECT  yfmf.*, \n");
        sql.append("(select * from youmu_fund_manager fm where fm.is_delete = 0 and fm.parent_id is null and fm.fund_user_id = yfmf.fund_user_id) manager_name \n");
        sql.append(" FROM youmu_fund_manager_fund yfmf \n");
        sql.append("WHERE  yfmf.is_delete = 0\n");
        sql.append("AND yfmf.is_delete = 0\n");
        sql.append("AND yfmf.id =: fundId\n");

        List<SearchYoumuFundManagerFund> searchYoumuFundManagerFunds = em.createNativeQuery(sql.toString(), SearchYoumuFundManagerFund.class)
                .setParameter("fundId", id)
                .getResultList();
        if (searchYoumuFundManagerFunds != null && searchYoumuFundManagerFunds.size() > 0) {
            return searchYoumuFundManagerFunds.get(0);
        }
        return null;
    }

    @Override
    public List<YoumuFundManagerFund> findByManagerId(Long managerId) {
        String sql = "from YoumuFundManagerFund where is_delete = 0 and fund_manager_id =:managerId";

        List<YoumuFundManagerFund> YoumuFundManagerFundList = em.createQuery(sql, YoumuFundManagerFund.class)
                .setParameter("managerId", managerId)
                .getResultList();
        return YoumuFundManagerFundList;
    }

    @Override
    public SearchYoumuFundManagerFund findByFundName(String name) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT yfmf.*, yfm.manager_name from \n");
        sql.append(" youmu_fund_manager_fund yfmf left join youmu_fund_manager yfm \n");
        sql.append(" on yfm.id = yfmf.fund_manager_id \n");
        sql.append(" where yfmf.is_delete = 0 and yfmf.is_delete = 0 and yfmf.fund_name =:name \n");

        List<SearchYoumuFundManagerFund> SearchYoumuFundManagerFunds = em.createNativeQuery(sql.toString(), SearchYoumuFundManagerFund.class)
                .setParameter("name", name)
                .getResultList();
        if (SearchYoumuFundManagerFunds != null && SearchYoumuFundManagerFunds.size() > 0) {
            return SearchYoumuFundManagerFunds.get(0);
        }
        return null;
    }

    @Override
    public PageT fundListByfundUserId(Long fundUserId, int num, int size) {
        String sql = "from YoumuFundManagerFund where is_delete = 0 and fund_user_id =:fundUserId and parent_id is null order by add_time desc";

        Query query  = em.createQuery(sql, YoumuFundManagerFund.class);
        query.setParameter("fundUserId", fundUserId);

        PageT pageT = new PageT();
        pageT.setCount(query.getResultList().size());
        pageT.setPageList(query.setFirstResult(num * size)
                .setMaxResults(size)
                .getResultList());
        return pageT;
    }

    @Override
    public PageT fundHistoryByfundId(Long fundUserId, int num, int size, Long fundId) {
        String sql = "from YoumuFundManagerFund where is_delete = 0 and fund_user_id =:fundUserId and( parent_id =:fundId or id = :fundId2)  order by add_time desc";

        Query query  = em.createQuery(sql, YoumuFundManagerFund.class);
        query.setParameter("fundUserId", fundUserId);
        query.setParameter("fundId", fundId);
        query.setParameter("fundId2", fundId);

        PageT pageT = new PageT();
        pageT.setCount(query.getResultList().size());
        pageT.setPageList(query.setFirstResult(num * size)
                .setMaxResults(size)
                .getResultList());
        return pageT;
    }
}
